package com.leyou.controller;

import com.leyou.pojo.Cart;
import com.leyou.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 购物车的新增
     *
     * @param cart
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart) {
        this.cartService.addCart(cart);
        return ResponseEntity.ok().build();
    }

    /**
     * 购物车的查询
     *
     * @param userId
     * @return
     */
    @GetMapping
    public ResponseEntity<List<Cart>> queryCartByUserId(@RequestParam("userId") Long userId) {
        List<Cart> cartList = this.cartService.queryCartByUserId(userId);
        if (null != cartList && 0 != cartList.size()) {
            return ResponseEntity.ok().body(cartList);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 登陆后商品数量的增加
     *
     * @param cart
     * @return
     */
    @PutMapping("increment")
    public ResponseEntity<Void> incrementCart(@RequestBody Cart cart) {
        this.cartService.incrementCart(cart);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 登陆后商品数量的减少
     *
     * @param cart
     * @return
     */
    @PutMapping("decrement")
    public ResponseEntity<Void> decrementCart(@RequestBody Cart cart) {
        this.cartService.decrementCart(cart);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 登陆后商品数量的删除
     *
     * @param userId
     * @param skuId
     * @return
     */
    @DeleteMapping("delete")
    public ResponseEntity<Void> deleteCart(@RequestParam("userId") Long userId, @RequestParam("skuId") Long skuId) {
        this.cartService.deleteCart(userId, skuId);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
